package com.jbm.jbmsupplier.net;

import android.os.Bundle;
import android.os.Handler;

import com.example.weather.myframe.Log;
import com.jbm.core.iface.IAcross;
import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.LoginEntity;
import com.jbm.onehome.iface.enums.AttachmentType;


/**
 * Created by 家帮忙 on 2017/5/21.
 */

public class NetinterSend {
    private final OHInterface Ohinterface;
    private Handler handler;
    private Bundle bundle;
    private final String CODE_SEND="code_send";
    private final String SEND_TEXT="发送文本消息";
    private final String SEND_PIC="发送图片";
    private final String SEND_AUDIO="发送语音";
    private final String GET_MESSAGE="获取信息";

    public NetinterSend(Handler handler) {
        this.handler = handler;
        Ohinterface = OHInterface.getG_server();
    }
    /**
     * 发送文本消息
     * 2017年5月21日14:29:47
     */

    public void SetSendTextMess(int cgid,int toid,int sedmess,String textmess){
        bundle=new Bundle();
        bundle.putString(CODE_SEND,SEND_TEXT);
        bundle.putString("textmess",textmess);
        bundle.putInt("sedmess",sedmess);
        bundle.putInt("toid",toid);
        bundle.putInt("cgid",cgid);
        this.setConnetSend();
    }

    /**
     * 发送图片
     * 2017年5月26日 09:37:47
     */
    public void SetSendImage(int tagimage,int toid,String filepath,String filename,int cgid){
        bundle=new Bundle();
        bundle.putString(CODE_SEND,SEND_PIC);
        bundle.putInt("tagimage",tagimage);
        bundle.putInt("toid",toid);
        bundle.putString("filepath",filepath);
        bundle.putString("filename",filename);
        bundle.putInt("cgid",cgid);
        this.setConnetSend();


    }
/**
 * 发送语音消息
 * 2017年5月26日 09:39:04
 */
    public void SetSendAudio(int cgid,int tagaudio,int toid,String filepath,String filename){

        bundle=new Bundle();
        bundle.putString(CODE_SEND,SEND_AUDIO);
        bundle.putInt("tagaudio",tagaudio);
        bundle.putInt("toid",toid);
        bundle.putString("filepath",filepath);
        bundle.putString("filename",filename);
        bundle.putInt("cgid",cgid);
        this.setConnetSend();


    }

    /**
     * 获取好友信息
     */
    public void getUerMessage(int loginid,int Message){
        bundle=new Bundle();
        bundle.putString(CODE_SEND,GET_MESSAGE);
        bundle.putInt("loginid",loginid);
        bundle.putInt("Message",Message);
        this.setConnetSend();


    }

    /**
     * 发送消息的链接
     */
    private void setConnetSend(){

        try {
            new Thread(SocketConnet).start();
        }catch (Exception e){
            Log.e("main",e);
        }




    }
    private Runnable SocketConnet=new Runnable() {
        @Override
        public void run() {
            String code = bundle.getString(CODE_SEND);
            switch (code) {
                case "发送文本消息":{
                    IAcross ars;
                    if (bundle.getInt("cgid")==-1){
                         ars =Ohinterface.SendText(bundle.getInt("toid"), "app_mobile", bundle.getString("textmess"));
                    }else{
                        ars =Ohinterface.SendText(bundle.getInt("cgid"),bundle.getInt("toid"), "app_mobile", bundle.getString("textmess"));

                    }

                    handler.sendMessage(handler.obtainMessage(bundle.getInt("sedmess"),ars));
                    break;
                }
                case"发送图片":{
                    IAcross senpin;

                    if (bundle.getInt("cgid")==-1){
                        senpin = Ohinterface.SendFile(bundle.getInt("toid"), "app_mobile", bundle.getString("filepath"), bundle.getString("filename"), AttachmentType.pic+"");

                    }else{
                        senpin = Ohinterface.SendFile(bundle.getInt("cgid"),bundle.getInt("toid"), "app_mobile", bundle.getString("filepath"), bundle.getString("filename"), AttachmentType.pic+"");

                    }

                    handler.sendMessage(handler.obtainMessage(bundle.getInt("tagimage"),senpin));
                break;
            }
                case"发送语音":{
                    IAcross sendaudio;
                    if (bundle.getInt("cgid")==-1){
                        sendaudio = Ohinterface.SendFile(bundle.getInt("toid"), "app_mobile", bundle.getString("filepath"), bundle.getString("filename"), AttachmentType.audio+"");

                    }else{
                        sendaudio = Ohinterface.SendFile(bundle.getInt("cgid"),bundle.getInt("toid"), "app_mobile", bundle.getString("filepath"), bundle.getString("filename"), AttachmentType.audio+"");

                    }



                    handler.sendMessage(handler.obtainMessage(bundle.getInt("tagaudio"),sendaudio));
                break;

            }
                case"获取信息":{
                    LoginEntity logins = OHInterface.getG_server().getLoginInfo(bundle.getInt("loginid"));
                    handler.sendMessage(handler.obtainMessage(bundle.getInt("Message"),logins));
                break;

            }
//            case"获取好友"{
//                break;
//
//            }
//            case"获取好友"{
//                break;
//
//            }
//            case"获取好友"{
//                break;
//
//            }
//            case"获取好友"{
//                break;
//
//            }
//            case"获取好友"{
//                break;
//
//            }
//            case"获取好友"{
//                break;
//
//            }

            }
        }
    };

}
